package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.form.impl.FootMissionSearchForm;
import org.footballmanager.pandafootball.system.service.FootMissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@SessionAttributes("footMissionSearchForm")
@RequestMapping("/foot/footMission")
public class FootMissionController extends BaseController {
    /* 实体名称 */
    private static final String ENTITYNAME = "footMission";
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);

    @Autowired
    private FootMissionService footMissionService;

    /**
     * 初始化
     *
     * @param map                   参数
     * @param footMissionSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootMissionSearchForm footMissionSearchForm) {
        FootMissionSearchForm newFootMissionSearchForm = new FootMissionSearchForm();
        if (footMissionSearchForm != null) {
            newFootMissionSearchForm = footMissionSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootMissionSearchForm);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, @ModelAttribute FootMissionSearchForm footMissionSearchForm) {
        footMissionService.search(ENTITYNAME, map, footMissionSearchForm);
        return LISTPAGE;
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootMissionSearchForm footMissionSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footMissionSearchForm);
        map.addAttribute(ENTITYNAME, footMissionService.get(id));
        return VIEWPAGE;
    }

}
